
package com.catsic.zcjc.web.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.catsic.base.annotation.Log;
import com.catsic.base.controller.BaseController;
import com.catsic.base.tree.TreeInfo;
import com.catsic.pub.drawer.service.DrawerService;
import com.catsic.pub.form.DrawerForm;
import com.catsic.zcjc.condition.SystemCondition;
import com.catsic.zcjc.constants.ZcjcConstants;
import com.catsic.zcjc.form.ListForm;
import com.catsic.zcjc.form.SystemForm;
import com.catsic.zcjc.service.SystemService;


@Controller
@RequestMapping("/system")
public class SystemController extends BaseController{
	
	@Autowired
	private SystemService systemService;
	
	@Autowired
	private DrawerService drawerService;

	
    /**
     * 查询系统
	 * @author xietiebing
	 * @param condition 
	 * @return
	 */
	@RequestMapping(value = "/query")
	@ResponseBody
	public Map<String,Object> query(SystemCondition condition,Pageable  pageable){
		Page<SystemForm> page = systemService.findAll(condition, pageable);
		
		List<SystemForm> list = page.getContent();
		if(CollectionUtils.isNotEmpty(list)){
			for(SystemForm form : list){
				systemService.findFormatDrawer(form);
			}
		}
		
		Map<String,Object> map = new HashMap<String,Object>(); 
		map.put(CONTENT, page);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * create:创建
	 * @author xietiebing
	 * @param form
	 * @return
	 */
	@RequestMapping(value = "/create" , method = RequestMethod.POST)
	@ResponseBody
	@Log(module = "系统管理", operation = "添加系统")
	public Map<String,Object> create(SystemForm form){
		form.setCreateId(getCurrentUser().getUserId());
		form.setCreateName(getCurrentUser().getUsername());
		
		form = systemService.add(form);
		Map<String,Object> map = new HashMap<String,Object>(); 
		map.put(CONTENT, form);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * update:更新
	 * @author xietiebing
	 * @param form
	 * @return
	 */
	@RequestMapping(value = "/update" , method = RequestMethod.POST)
	@ResponseBody
	@Log(module = "系统管理", operation = "修改系统")
	public Map<String,Object> update(SystemForm form){
		form = systemService.update(form);
		Map<String,Object> map = new HashMap<String,Object>(); 
		map.put(CONTENT, form);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	
	/**
	 * detail:详情
	 * @author xietiebing
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/detail" , method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> detail(String id){
		SystemForm form =systemService.findSystemById(id);
		Map<String,Object> map = new HashMap<String,Object>(); 
		map.put(CONTENT, form);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * delete:删除
	 * @author xietiebing
	 * @param id
	 * @return 
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	@Log(module = "系统管理", operation = "删除系统")
	public Map<String,Object> delete(String id){
		systemService.delete(id);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(RESULT,RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * detail:系统类型列表
	 * @author xietiebing
	 * @param 
	 * @return
	 */
	@RequestMapping(value = "/typeList" , method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> typeList(){
		List<DrawerForm> drawerList = drawerService.findByParentCode(ZcjcConstants.DRAWER_SYSTEM_TYPE);
		
		List<ListForm> list = new ArrayList<ListForm>();
		for(DrawerForm drawer : drawerList){
			ListForm form = new ListForm();
			form.setId(drawer.getValue());
			form.setText(drawer.getName());
			
			list.add(form);
		}
		
		Map<String,Object> map = new HashMap<String,Object>(); 
		map.put(CONTENT, list);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * tree:系统树
	 * @author xietiebing
	 * @param form
	 * @return
	 */
	@RequestMapping(value = "/tree")
	@ResponseBody
	public Map<String,Object> tree(String id){
		List<TreeInfo> form = systemService.getTreeInfo(id);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT,form);
		map.put(RESULT,RESULT_SUCCESS);
		return map;
	}
	

}
